﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class invoice : System.Web.UI.Page
{

    List<Bestlife.ERP.Entities.Cart_Item> shp = new List<Bestlife.ERP.Entities.Cart_Item>();
    Bestlife.ERP.BusinessRules.Shopping_Cart shoppingCheck;
    Boolean transact_Ok = false;
    string invoiceids = "";
    string uid = "0";
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string invoiceNo = Request.QueryString["rcptID"];
            //Bestlife.ERP.DataAccess.ReturncountForRecipt rsp = new Bestlife.ERP.DataAccess.ReturncountForRecipt();

            //string invoiceids = rsp.ReturnReciptID().ToString();
            //string invoiceNo = "BC" + invoiceids;
            InvNo.Text = invoiceNo;
            Date.Text = System.DateTime.Now.Day.ToString() + "/" + System.DateTime.Now.Month.ToString() + "/" + System.DateTime.Now.Year.ToString();
            string uid = Session["user"].ToString();
            //string uid = "7";

            string check_Ename = uid;

            int id_length = uid.Length;

            if (id_length > 2)
            {
                check_Ename = uid.Substring(0, 2);
            }
            else
            {
                check_Ename = uid;
            }


            if (check_Ename.Equals("EM"))
            {
                Bestlife.ERP.DataAccess.D_Employee demp = new Bestlife.ERP.DataAccess.D_Employee();
                Bestlife.ERP.Entities.E_Employee eemp = new Bestlife.ERP.Entities.E_Employee();
                eemp=demp.Employee_return(uid);
                Name.Text = eemp.Employee_name;
                ID.Text = "" + uid;
            }
            else if (idisok(uid)) 
            {
                Name.Text = uid;
                ID.Text = "ID Number :" + uid;
            }
            else
            {
                Bestlife.ERP.Entities.E_Profile pro = Bestlife.ERP.DataAccess.DistributerAC.getallprofileinfo(uid);
                Name.Text = pro.Title1 + pro.Name1;
                ID.Text = "BLAA :"+uid;
            }

            if (Session["cashierpos"] == null)
            {
                Response.Redirect("Admin_Login.aspx");
            }
            else { cashier.Text = Session["cashierpos"].ToString(); }
            

            reBind();
        }
        catch (Exception) 
        { 
        
        }
    }

    #region Rebind

    public void reBind()
    {
        List<Bestlife.ERP.Entities.Return_to_R> listrtn = new List<Bestlife.ERP.Entities.Return_to_R>();
        List<Bestlife.ERP.Entities.Tocheck_To_cart> listrtntocheck = new List<Bestlife.ERP.Entities.Tocheck_To_cart>();
        listrtn.Clear();
        listrtntocheck.Clear();
        if (Session["cart"] != null)
        {
            shoppingCheck = (Bestlife.ERP.BusinessRules.Shopping_Cart)Session["cart"];

            TotAmount.Visible = false;
            totpv.Visible = false;
            totbv.Visible = false;
            if (shoppingCheck.Items.Count > 0)
            {
                TotAmount.Visible = true;
                totpv.Visible = true;
                totbv.Visible = true;

                #region to recipt
                listrtn = shoppingCheck.reciptvalues;
                GridView1.DataSource = listrtn;
                GridView1.DataBind();
                Bestlife.ERP.DataAccess.Pricecheckup chkp = new Bestlife.ERP.DataAccess.Pricecheckup();

                TotAmount.Text = "Rs." + chkp.returnPrice(shoppingCheck.total.ToString());
                totpv.Text = "" + Math.Round(shoppingCheck.totalPoint_value,2);
                totbv.Text = "" + Math.Round(shoppingCheck.totalBusiness_Value,2);
                Label3.Text = "Rs." +chkp.returnPrice(Math.Round(shoppingCheck.Tot_discounttot,2).ToString());
                Label1.Text = "Rs." + chkp.returnPrice(Math.Round(shoppingCheck.total_Rprice, 2).ToString());
                Label2.Text = "" + shoppingCheck.total_quantity;

                #endregion

            }
        }
    }
    #endregion


    protected void Button1_Click(object sender, EventArgs e)
    {
        HttpContext.Current.Response.Redirect("backtoshopingwithend.aspx");
    }

    public Boolean idisok(string ids)
    {
        Boolean tf = false;

        int idlength = ids.Length;
        string lastch = ids.Substring(idlength - 1);

        if ((lastch.Equals("V") || lastch.Equals("v") || lastch.Equals("X") || lastch.Equals("x")) && (ids.Length == 10))
        {
            tf = true;
        }

        return tf;
    }
}